package com.example.ware.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.ware.annotation.Token;
import com.example.ware.dao.DeclareMapper;
import com.example.ware.dao.RecordMapper;
import com.example.ware.model.Spage.ShowPage;
import com.example.ware.model.po.Access;
import com.example.ware.model.po.Admin;
import com.example.ware.model.po.Employees;
import com.example.ware.model.po.Record;
import com.example.ware.model.responseData.ResponseData;
import com.example.ware.model.vo.AccessVo;
import com.example.ware.model.vo.DeclareVo;
import com.example.ware.model.vo.PformVo;
import com.example.ware.model.vo.RecordVo;
import com.example.ware.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@CrossOrigin
@RestController
@RequestMapping("/record")
public class RecordController {

    @Autowired
    private AccessService accessService;

    @Resource
    RedisTemplate redisTemplate;

    @Resource
    RecordMapper recordMapper;

    @Autowired
    private RecordService recordService;

    @Autowired
    private PlaceService placeService;

    @Autowired
    private FireWorksService fireWorksService;

    @Autowired
    private KindsService kindsService;

    /**
     * 出库提交的表单
     */
    @PostMapping("/submit")
    public ResponseData submit(@RequestBody PformVo pform) {

        System.out.println(pform);
        List<Access> accessList=pform.getAccessList();
        Record record=pform.getRecord();


        Record r=recordMapper.getOne(record.getDid());
        if (r==null) {
            return new ResponseData(null, true, "找不到表单", 404);
        }


        int i = accessService.submitAccess(accessList, r);
        if (i == 1) {
            return new ResponseData(null, true, "提交成功", 200);
        } else {
            return new ResponseData(null, true, "提交失败", 201);
        }
    }


    /**
     * 获取进出仓库的历史记录
     * 参数：时间，类型
     */
    @Token
    @GetMapping("/list")
    public ResponseData getList(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "3") Integer pageSize,
                                String cid, String sort, @RequestHeader String token) {

        System.out.println("9999999999999999999999999999999999999999999999");
        System.out.println(pageSize);


        Admin admin = (Admin) redisTemplate.opsForValue().get("Admin_" + token);
        if (admin.getWid() == null) {
            return new ResponseData(null, false, "您非仓库专员", 201);

        }

        ShowPage showPage = new ShowPage();
        Integer pageBegin = (page - 1) * pageSize;
        showPage.setPageNum(page);
        //总数据条数
//        QueryWrapper<Record> qw=new QueryWrapper<>();
//        qw.eq("wid",admin.getWid());
//        showPage.setSize(recordService.list(qw).size());
        showPage.setSize(recordMapper.getReListAll(cid, admin.getWid()).size());
        //每页数据条数
        showPage.setPageSize(pageSize);
        //总页数
        Integer div = showPage.getSize() / pageSize;
        Integer pages = showPage.getSize() % pageSize == 0 ? div : div + 1;
        showPage.setPages(pages);

        List<RecordVo> recordVoList = recordMapper.getReList(sort, cid, admin.getWid(), pageBegin, pageSize);

        List<RecordVo> res = new ArrayList<>();
        for (RecordVo r : recordVoList) {

            QueryWrapper<Access> qq = new QueryWrapper<>();
            qq.eq("rid", r.getId());
            List<Access> list = accessService.list(qq);
            List<AccessVo> accessVos = new ArrayList<>();
            for (Access a : list) {
                System.out.println("888888888888888888888888888888888888888888888888888888888");
                System.out.println(a);
                AccessVo accessVo = new AccessVo();
                accessVo.setId(a.getId());
                accessVo.setAdds(a.getAdds());
                accessVo.setReduces(a.getReduces());
                accessVo.setPname(placeService.getById(a.getPid()).getName());
                accessVo.setKname(fireWorksService.getById(a.getFid()).getName());
                accessVos.add(accessVo);
            }
            r.setAccessVoList(accessVos);

            res.add(r);
        }

        showPage.setData(res);

        return new ResponseData(showPage, true, "获取成功", 200);
    }


    //    获取对应表单的历史记录
    @GetMapping("/getOne")
    public ResponseData getList(String did) {
        Record record=recordMapper.getOne(did);
        if(record==null){
            return new ResponseData(null,false,"暂未填写表单",200);
        }
        RecordVo recordVo=new RecordVo();
        recordVo.setId(record.getId());
        recordVo.setDid(did);
        recordVo.setEntrytime(record.getEntrytime());
        recordVo.setName("进出货");
        recordVo.setSubtime(record.getSubtime());

        QueryWrapper<Access> qw1=new QueryWrapper<>();
        qw1.eq("rid",record.getId());
        List<Access> accessList=accessService.list(qw1);

        List<AccessVo> accessVos = new ArrayList<>();
        for (Access a : accessList) {
            System.out.println("888888888888888888888888888888888888888888888888888888888");
            System.out.println(a);
            AccessVo accessVo = new AccessVo();
            accessVo.setId(a.getId());
            accessVo.setAdds(a.getAdds());
            accessVo.setReduces(a.getReduces());
            accessVo.setPname(placeService.getById(a.getPid()).getName());
            accessVo.setKname(fireWorksService.getById(a.getFid()).getName());
            accessVos.add(accessVo);
        }
        recordVo.setAccessVoList(accessVos);

        return new ResponseData(recordVo,true,"获取成功",200);
    }






}
